package net.streaming.client;

import java.io.IOException;
import java.io.InputStream;

public class ReadThread extends Thread {
	private InputStream in ;
	private Handle handle;
	
	public ReadThread(InputStream in, Handle handle) {
		this.in = in;
		this.handle = handle;
	}

	public void run() {
		try {
			this.receiveStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void receiveStream() throws IOException{
		byte[] buf = new byte[1024*1024];
		int len = 0;
		while(!Thread.interrupted()){
			len = in.read(buf);
			if(len>0){
				byte[] msgBuf = new byte[len];
				System.arraycopy(buf, 0, msgBuf, 0, len);
				this.handle.handle(msgBuf);
			}
		}
	}

	public void stopLoop() {
		this.interrupt();
	}
	
}
